    <!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="author" content="nodejh">
		<meta name="description" content="Describe your website">
		<meta name="generator" content="Hugo 0.16" />
		<title>Get the Max Number in Array &middot; nodejh</title>
		<link rel="shortcut icon" href="http://nodejh.com/images/favicon.ico">
		<link rel="stylesheet" href="http://nodejh.com/css/style.css">
		
		<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/atom-one-light.min.css">
		

		
		<link rel="stylesheet" href="http://nodejh.com/fontawesome/css/font-awesome.min.css">
		

		
		<link href="http://nodejh.com/index.xml" rel="alternate" type="application/rss+xml" title="nodejh" />
		
	</head>

    <body>
       <nav class="main-nav">
	
	
		<a href='http://nodejh.com/'> <span class="arrow">←</span>Home</a>
	

	
		<a href='http://nodejh.com/post'>Archive</a>
	

	
		<a href='http://nodejh.com/about'>About</a>
	

	
	<a class="cta" href="http://nodejh.com/index.xml">Subscribe</a>
	
</nav>


        <section id="wrapper">
            <article class="post">
                <header>
                    <h1>Get the Max Number in Array</h1>
                    <h2 class="headline">
                    Oct 24, 2016 19:20
                    · 83 words
                    · 1 minute read
                      <span class="tags">
                      
                      
                          
                              <a href="http://nodejh.com/tags/javascript">JavaScript</a>
                          
                              <a href="http://nodejh.com/tags/algorithm">Algorithm</a>
                          
                      
                      
                      </span>
                    </h2>
                </header>
                <section id="post-body">
                    

<p>本文介绍 JavaScript 的几种从数组中找出最大值的方法。</p>

<h4 id="使用递归函数">使用递归函数</h4>

<pre><code>var arr = [9,8,55,66,49,68,109,55,33,6,2,1];
var max = arr[0];
function findMax( i ){
  if( i == arr.length ) return max;
  if( max &lt; arr[i] ) max = arr[i];
  findMax(i+1);
}

findMax(1);
console.log(max);
</code></pre>

<h4 id="使用-for-循环遍历">使用 for 循环遍历</h4>

<pre><code>var arr = [9,8,55,66,49,68,109,55,33,6,2,1];  
var max = arr[0];
for(var i = 1; i &lt; arr.length; i++){
  if( max &lt; arr[i] ){
    max = arr[i];
  }
}

console.log(max);
</code></pre>

<h4 id="使用apply将数组传入max方法中直接返回">使用apply将数组传入max方法中直接返回</h4>

<pre><code>Math.max.apply(null,[9,8,55,66,49,68,109,55,33,6,2,1])
</code></pre>

<p>除此之外，还有很多数组排序方式，都可以在排序后，根据新数组索引值获取 最大/最小 值。</p>

<pre><code>var a=[1,2,3,5];
console.log(Math.max.apply(null, a));//最大值
console.log(Math.min.apply(null, a));//最小值
</code></pre>

<p>多维数组可以这么修改：</p>

<pre><code>var a=[1,2,3,[5,6],[1,4,8]];
var ta=a.join(&quot;,&quot;).split(&quot;,&quot;);//转化为一维数组
console.log(Math.max.apply(null,ta));//最大值
console.log(Math.min.apply(null,ta));//最小值
</code></pre>

                </section>
            </article>

            <div id="disqus_thread"></div>
<div style="margin-top: 50px">
  评论: 请点击 Gtihub Issue 链接跳转到评论页面
</div>
</div>


            <footer id="footer">
    
        <div id="social">

	
	
    <a class="symbol" href="https://www.facebook.com/nodejh">
        <i class="fa fa-facebook-square"></i>
    </a>
    
    <a class="symbol" href="https://www.github.com/nodejh">
        <i class="fa fa-github"></i>
    </a>
    
    <a class="symbol" href="http://stackoverflow.com/users/4518882/nodejh">
        <i class="fa fa-stack-overflow"></i>
    </a>
    
    <a class="symbol" href="https://www.twitter.com/nodejh">
        <i class="fa fa-twitter"></i>
    </a>
    


</div>

    
    <p class="small">
    
        © Copyright 2016 <i class="fa fa-heart" aria-hidden="true"></i> nodejh
    
    </p>
</footer>

        </section>

        <script   src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="   crossorigin="anonymous"></script>
<script src="http://nodejh.com/js/main.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>




<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-84989670-1', 'auto');
ga('send', 'pageview');
</script>


    </body>
</html>
